package com.zz.yt.test.net;


import androidx.annotation.NonNull;

import com.blankj.utilcode.util.GsonUtils;
import com.whf.android.jar.RetrofitT;
import com.whf.android.jar.base.delegate.IPageLoadListener;
import com.whf.android.jar.constants.BaseEntries;
import com.whf.android.jar.net.callback.IError;
import com.whf.android.jar.net.callback.ISuccess;
import com.whf.android.jar.net.rx.t.RxRetrofit;
import com.whf.android.jar.net.rx.t.RxService;
import com.whf.android.jar.tool.Observer;


import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 单独的 网络请求
 *
 * @author gf
 * @version 1.0
 */
public class NextRetrofit extends RxRetrofit {

    public NextRetrofit(IError iError) {
        super(iError);
    }

    public NextRetrofit(IError iError, IPageLoadListener iPageLoadListener) {
        super(iError, iPageLoadListener);
    }

    @NonNull
    public static NextRetrofit init(IError iError) {
        return new NextRetrofit(iError);
    }

    /**
     * 获取  根据id获取技术支持 详情 列表
     */
    public void getTaKen(String url, WeakHashMap<String, Object> params, final ISuccess listener) {
        if (getRetrofit() == null) {
            return;
        }
        WeakHashMap<String, Object> lang = new WeakHashMap<>();
        if (params == null) {
            params = new WeakHashMap<>();
        }
        RxService service = RetrofitT.getRetrofit().create(RxService.class);
        RequestBody mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),
                GsonUtils.toJson(params));
        setSubscribe(service.post(lang, url, mBody), new Observer<BaseEntries<Object>>() {
            @Override
            public void onNext(BaseEntries<Object> entries) {
                if (entries != null) {
                    if (entries.isOk()) {
                        if (listener != null) {
                            listener.onSuccess(GsonUtils.toJson(entries.getData()));
                        }
                    } else {
                        if (iError != null) {
                            iError.onError(entries.getCode(), entries.getMessage());
                        }
                    }
                }
            }

            @Override
            public void onError(int code, String error) {
                if (iError != null) {
                    iError.onError(code, error);
                }
            }
        });
    }

}
